package ch.ti.bfh.app.loodel.internal;

import ch.ti.bfh.app.loodel.domain.member.Member;
import ch.ti.bfh.app.loodel.domain.member.MemberRepository;
import ch.ti.bfh.app.loodel.service.MemberData;
import ch.ti.bfh.app.loodel.service.MemberRegistrationService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.util.List;


@Service
@Transactional
public class DefaultMemberRegistrationServiceImpl implements MemberRegistrationService {
    private static final List<GrantedAuthority> DEFAULT_AUTHORITIES = AuthorityUtils.createAuthorityList("ROLE_USER");

    private final MemberRepository memberRepository;

    @Inject
    public DefaultMemberRegistrationServiceImpl(MemberRepository memberRepository) {
        this.memberRepository = memberRepository;
    }

    private MemberData getMemberData(Member member) {
        return new MemberData(member.getEmail(), member.getFullName());
    }

    @Override
    public MemberData findMemberByEMail(String mail) {
        Member member = this.memberRepository.findByEmail(mail);
        if (member == null) {
            return null;
        }
        return getMemberData(member);
    }

    @Override
    public void registerNewMember(String mail, String fullName) {
        this.memberRepository.save(new Member(mail, "", "", fullName));
    }

}
